package com.mrobuy.jd;

import com.gavel.database.SQLExecutor;
import com.google.gson.Gson;
import com.jd.open.api.sdk.JdClient;
import com.jd.open.api.sdk.request.supplier.UserCategory3InfoGetRequest;
import com.jd.open.api.sdk.response.supplier.UserCategory3Dto;
import com.jd.open.api.sdk.response.supplier.UserCategory3InfoGetResponse;
import com.mrobuy.jdvc.entity.VCCategory;

public class VCCategotyLoader {

    public static void main(String[] args) throws Exception {

        load(JdClientFactory.jdClient());

    }

    private static void load(JdClient client) throws Exception {

        UserCategory3InfoGetRequest request=new UserCategory3InfoGetRequest();

        UserCategory3InfoGetResponse response = client.execute(request);

        System.out.println(new Gson().toJson(response));

        for (UserCategory3Dto userCategory3Dto : response.getUserCategory3Info().getUserCategory3Dtos()) {


            VCCategory vcCategory = null;
            try {
                vcCategory =  SQLExecutor.executeQueryBean("select * from VC_CATEGORY where ID = ? ", VCCategory.class, userCategory3Dto.getCode());
            } catch (Exception e) {
                e.printStackTrace();
            }

            boolean insert = (vcCategory == null);
            if ( insert ) {
                vcCategory = new VCCategory();
                vcCategory.setId(Integer.parseInt(userCategory3Dto.getCode()));
            }
            vcCategory.setLevel(3);

            vcCategory.setName(userCategory3Dto.getName());

            if ( insert ) {
                SQLExecutor.insert(vcCategory);
            } else {
                SQLExecutor.update(vcCategory);
            }

        }

    }
}
